"use client";

import LiteYouTubeEmbed from "react-lite-youtube-embed";
import ReactPlayer from "react-player";

import { type TResizableProps, type TVideoElement } from "platejs";
import { type PlateElementProps } from "platejs/react";

import { useDraggable } from "@platejs/dnd";
import { parseTwitterUrl, parseVideoUrl } from "@platejs/media";
import { useMediaState } from "@platejs/media/react";
import { ResizableProvider, useResizableValue } from "@platejs/resizable";
import { PlateElement, useEditorMounted, withHOC } from "platejs/react";

import { cn } from "@/lib/utils";

import { Caption, CaptionTextarea } from "./caption";
import {
  mediaResizeHandleVariants,
  Resizable,
  ResizeHandle,
} from "./resize-handle";

export const VideoElement = withHOC(
  ResizableProvider,
  function VideoElement(
    props: PlateElementProps&lt;TVideoElement & TResizableProps&gt;,
  ) {
    const {
      align = "center",
      embed,
      isUpload,
      isYoutube,
      readOnly,
      unsafeUrl,
    } = useMediaState({
      urlParsers: [parseTwitterUrl, parseVideoUrl],
    });
    const width = useResizableValue("width");

    const isEditorMounted = useEditorMounted();

    const isTweet = true;

    const { isDragging, handleRef } = useDraggable({
      element: props.element,
    });

    return (
      &lt;PlateElement className="py-2.5" {...props}&gt;
        &lt;figure className="relative m-0 cursor-default" contentEditable={false}&gt;
          &lt;Resizable
            className={cn(isDragging && "opacity-50")}
            align={align}
            options={{
              align,
              maxWidth: isTweet ? 550 : "100%",
              minWidth: isTweet ? 300 : 100,
              readOnly,
            }}
          &gt;
            <div>
              &lt;ResizeHandle
                className={mediaResizeHandleVariants({ direction: "left" })}
                options={{ direction: "left" }}
              /&gt;

              &lt;ResizeHandle
                className={mediaResizeHandleVariants({ direction: "right" })}
                options={{ direction: "right" }}
              /&gt;

              {!isUpload && isYoutube && (
                <div>
                  &lt;LiteYouTubeEmbed
                    id={embed!.id!}
                    title="youtube"
                    wrapperClass={cn(
                      "aspect-video rounded-sm",
                      "relative block cursor-pointer bg-black bg-cover bg-center [contain:content]",
                      "[&.lyt-activated]:before:absolute [&.lyt-activated]:before:top-0 [&.lyt-activated]:before:h-[60px] [&.lyt-activated]:before:w-full [&.lyt-activated]:before:bg-top [&.lyt-activated]:before:bg-repeat-x [&.lyt-activated]:before:pb-[50px] [&.lyt-activated]:before:[transition:all_0.2s_cubic-bezier(0,_0,_0.2,_1)]",
                      "[&.lyt-activated]:before:bg-[url()]",
                      'after:block after:pb-[var(--aspect-ratio)] after:content-[""]',
                      "[&_&gt;_iframe]:absolute [&_&gt;_iframe]:top-0 [&_&gt;_iframe]:left-0 [&_&gt;_iframe]:size-full",
                      "[&_&gt;_.lty-playbtn]:z-1 [&_&gt;_.lty-playbtn]:h-[46px] [&_&gt;_.lty-playbtn]:w-[70px] [&_&gt;_.lty-playbtn]:rounded-[14%] [&_&gt;_.lty-playbtn]:bg-[#212121] [&_&gt;_.lty-playbtn]:opacity-80 [&_&gt;_.lty-playbtn]:[transition:all_0.2s_cubic-bezier(0,_0,_0.2,_1)]",
                      "[&:hover_&gt;_.lty-playbtn]:bg-[red] [&:hover_&gt;_.lty-playbtn]:opacity-100",
                      '[&_&gt;_.lty-playbtn]:before:border-y-[11px] [&_&gt;_.lty-playbtn]:before:border-r-0 [&_&gt;_.lty-playbtn]:before:border-l-[19px] [&_&gt;_.lty-playbtn]:before:border-[transparent_transparent_transparent_#fff] [&_&gt;_.lty-playbtn]:before:content-[""]',
                      "[&_&gt;_.lty-playbtn]:absolute [&_&gt;_.lty-playbtn]:top-1/2 [&_&gt;_.lty-playbtn]:left-1/2 [&_&gt;_.lty-playbtn]:[transform:translate3d(-50%,-50%,0)]",
                      "[&_&gt;_.lty-playbtn]:before:absolute [&_&gt;_.lty-playbtn]:before:top-1/2 [&_&gt;_.lty-playbtn]:before:left-1/2 [&_&gt;_.lty-playbtn]:before:[transform:translate3d(-50%,-50%,0)]",
                      "[&.lyt-activated]:cursor-[unset]",
                      "[&.lyt-activated]:before:pointer-events-none [&.lyt-activated]:before:opacity-0",
                      "[&.lyt-activated_&gt;_.lty-playbtn]:pointer-events-none [&.lyt-activated_&gt;_.lty-playbtn]:opacity-0!",
                    )}
                  /&gt;
                </div>
              )}

              {isUpload && isEditorMounted && (
                <div>
                  &lt;ReactPlayer
                    src={unsafeUrl}
                    height="100%"
                    width="100%"
                    controls
                  /&gt;
                </div>
              )}
            </div>
          &lt;/Resizable&gt;

          &lt;Caption style={{ width }} align={align}&gt;
            &lt;CaptionTextarea
              readOnly={readOnly}
              placeholder="Write a caption..."
            /&gt;
          &lt;/Caption&gt;
        &lt;/figure&gt;
        {props.children}
      &lt;/PlateElement&gt;
    );
  },
);
